﻿// FIV Fast Image viewer
// http://code.google.com/p/fiv/
// 
// Copyright 2013, Ken Rentz.
//
// This is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this software; if not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.
// 

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FIV
{
	public partial class ViewedImage : Form
	{
		private readonly BaseImage _Image;
		private readonly FileListManager _Files;

		public ViewedImage()
		{
			InitializeComponent();
//			bool dummy = Options.Instance.FitToWindow; // TODO cleanup singleton initialization

			_Image = new BaseImage();
			_Files = new FileListManager();

			SetSizes();
			EnableMenus();
		}

		private void EnableMenus()
		{
			bool filesLoaded = _Files.NumberOfFiles > 1;

			_NextMenuItem.Enabled = filesLoaded;
			_RandomMenuItem.Enabled = filesLoaded;
			_PreviousMenuItem.Enabled = filesLoaded;
		}

		private void ViewedImage_ResizeEnd(object sender, EventArgs e)
		{

			SetSizes();
			_PictureBox.Image = _Image.GetImage(_PictureBox.Size);
		}

		// we set the picture box size to the size of the image or the program window.
		private void SetSizes()
		{
			Size displayedImageSize;
			if (Options.Instance.FitToWindow)
				displayedImageSize = _TableLayoutPanel1.Size;
			else
				displayedImageSize = _Image.ActualImageSize;

System.Diagnostics.Debug.WriteLine("window size = " + _TableLayoutPanel1.Size);
System.Diagnostics.Debug.WriteLine("displayed picture size = " + displayedImageSize);
			_Image.AdjustDisplayedImageSize(displayedImageSize);
		}


		private void ViewFile()
		{
			SetSizes();
			Text = _Image.GetDescriptiveText();
			_PictureBox.Image = _Image.GetImage(_PictureBox.Size);
		}

		private void OpenMenuItem_Click(object sender, EventArgs e)
		{
			HandleOpen();
			EnableMenus();
		}

		private void NextMenuItem_Click(object sender, EventArgs e)
		{
			HandleNext();
		}

		private void RandomMenuItem_Click(object sender, EventArgs e)
		{
			HandleRandom();
		}

		private void PreviousMenuItem_Click(object sender, EventArgs e)
		{
			HandlePrevious();
		}

		private void ExitMenuItem_Click(object sender, EventArgs e)
		{
			HandleExit();
		}

		private void ConfigureOptionsMenuItem_Click(object sender, EventArgs e)
		{

			OptionsForm optionsForm = new OptionsForm();
			optionsForm.ShowDialog();
		}

		private void ViewedImage_KeyPress(object sender, KeyPressEventArgs e)
		{
			char key= char.ToUpper(e.KeyChar);

			switch (key)
			{
				case 'O':
					HandleOpen();
					break;
				case ' ':
					HandleNext();
					break;
				case '\r':
					HandleRandom();
					break;
				case '\b':
					HandlePrevious();
					break;
				case 'X':
					HandleExit();
					break;
			}
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Form about = new AboutBox();
			about.ShowDialog();
		}

	}
}
